package demo.jaxb;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class Java2XML {

	/**
	 * @param args
	 * @throws JAXBException 
	 */
	public static void main(String[] args) throws JAXBException {
		JAXBContext context = JAXBContext.newInstance(Person.class);

		Marshaller marshaller = context.createMarshaller();
		marshaller.setProperty(Marshaller.JAXB_ENCODING,"UTF-8");//编码格式
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);//是否格式化生成的xml串
		marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);//是否省略xml头信息（<?xml version="1.0" encoding="gb2312" standalone="yes"?>）
		
		Person person = new Person();
		List<People> list = new ArrayList<People>();
		for (int i = 0; i < 4; i++) {
	        People p1 = new People();
	        p1.id=i+"";
	        p1.name="tom"+i;
	        p1.age=i;
	        list.add(p1);
        }
		person.setPeople(list);
		
		marshaller.marshal(person, System.out);
	}

}
